package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.file.FileStorageService;
import com.heima.model.common.dto.PageResponseResult;
import com.heima.model.common.dto.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dto.WmPageRequestDto;
import com.heima.model.wemedia.pojo.WmMaterial;
import com.heima.model.wemedia.pojo.WmUser;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) throws IOException {
        WmUser user = WmThreadLocalUtil.getUser();
        if(user == null){
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }
        // 先上传文件到minio中，得到返回的url
        // 给文件名设置一个唯一值，而不是使用原始文件名。可以使用分布式唯一id生成算法生成：uuid或snowflake
        String fileName = UUID.randomUUID().toString().replace("-", "");
        // 原始文件名
        String originalFilename = multipartFile.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        fileName = fileName + suffix;
        String url = fileStorageService.uploadImgFile("", fileName, multipartFile.getInputStream());
        // 获取当前用户的id，构造WmMeterial对象
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUrl(url);
        wmMaterial.setUserId(user.getId());
        wmMaterial.setType((short) 0);
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setIsCollection((short) 0);
        // 入库
        this.save(wmMaterial);
        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public PageResponseResult queryByPage(WmPageRequestDto dto) {
        // 检查参数合法性
        dto.checkParam();
        // 分页参数
        Page<WmMaterial> page = new Page<>(dto.getPage(),dto.getSize());
        // 构造条件
        page = this.page(page,
                Wrappers.<WmMaterial>lambdaQuery()
                        .eq(WmMaterial::getIsCollection, dto.getIsCollection())
                        .eq(WmMaterial::getUserId, WmThreadLocalUtil.getUser().getId())
                        .orderByDesc(WmMaterial::getCreatedTime));
        PageResponseResult result = new PageResponseResult(
                                                (int) page.getCurrent(),
                                                (int) page.getSize(),
                                                (int) page.getTotal());
        result.setCode(AppHttpCodeEnum.SUCCESS.getCode());
        result.setErrorMessage(AppHttpCodeEnum.SUCCESS.getErrorMessage());
        result.setData(page.getRecords());
        return result;
    }
}
